import jieba

#打开文本文件
with open('我与地坛.txt','r',encoding='utf-8') as f:
    content = f.read()

#使用jieba进行分词
words = jieba.cut(content)

#去除标点符号
words = [word for word in words if word.isalnum()]

#去除单个字
words = [word for word in words if len(word) > 1]

#统计词频
word_freq = {}
for word in words:
    if word in word_freq:
        word_freq[word] +=1
    else:
        word_freq[word] = 1

#对词频进行排序
sorted_word_freq = sorted(word_freq.items(),key=lambda x:x[1],reverse=True)

#打开一个新的文件，并将排序后的词频写入新文件
with open('词频排序.txt','w',encoding='utf-8') as f:
    for word, freq in sorted_word_freq:
        f.write(f'{word}: {freq}\n')

#结果Building prefix dict from the default dictionary ...
#Loading model from cache C:\Users\86198\AppData\Local\Temp\jieba.cache
#Loading model cost 0.743 seconds.
#Prefix dict has been built successfully.